package com.codedancer.campus.manager.api.carousel;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.response.ApiResponse;
import com.codedancer.campus.manager.api.carousel.ro.CarouselAddOrUpdateRo;
import com.codedancer.campus.manager.api.carousel.ro.CarouselPageSearchRo;
import com.codedancer.campus.manager.service.ICarouselService;
import com.codedancer.campus.manager.service.bo.carousel.CarouselAddOrUpdateBo;
import com.codedancer.campus.manager.service.bo.carousel.CarouselPageSearchBo;
import com.codedancer.campus.manager.service.vo.carousel.CarouselPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/carousel")
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "轮播图Api", description = "轮播图Api", tags = "轮播图Api")
public class CarouselApi extends BaseApi {

    @Autowired
    private ICarouselService carouselService;

    @PostMapping("/add")
    @ApiOperation("添加轮播图")
    public ApiResponse<Void> add(
        @RequestBody @Valid @ApiParam(value = "添加轮播图的参数", required = true)
        CarouselAddOrUpdateRo carouselAddOrUpdateRo) {
        CarouselAddOrUpdateBo carouselAddOrUpdateBo = BeanUtil.copyProperties(carouselAddOrUpdateRo,
            CarouselAddOrUpdateBo.class);
        carouselService.add(carouselAddOrUpdateBo);
        return success();
    }

    @PutMapping("/update/{uuid}")
    @ApiOperation("修改轮播图")
    public ApiResponse<Void> update(@PathVariable(value = "uuid", required = true) String uuid,
                                    @RequestBody @Valid @ApiParam(value = "添加轮播图的参数", required = true)
                                    CarouselAddOrUpdateRo carouselAddOrUpdateRo) {
        CarouselAddOrUpdateBo carouselAddOrUpdateBo = BeanUtil.copyProperties(carouselAddOrUpdateRo,
            CarouselAddOrUpdateBo.class);
        carouselAddOrUpdateBo.setUuid(uuid);
        carouselService.update(carouselAddOrUpdateBo);
        return success();
    }

    @DeleteMapping("/delete/{uuid}")
    @ApiOperation("删除轮播图")
    public ApiResponse<Void> delete(@PathVariable(value = "uuid", required = true) String uuid) {
        carouselService.delete(uuid);
        return success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询轮播图")
    public ApiResponse<IPage<CarouselPageVo>> page(
        @ApiParam(value = "轮播图分页查询Ro", required = true)
        CarouselPageSearchRo carouselPageSearchRo) {
        CarouselPageSearchBo carouselPageSearchBo = BeanUtil.copyProperties(carouselPageSearchRo,
            CarouselPageSearchBo.class);
        return success(carouselService.page(carouselPageSearchBo));
    }

    @PutMapping("/enable/{uuid}")
    @ApiOperation("启用轮播图")
    public ApiResponse<Void> enable(@PathVariable(value = "uuid", required = true) @ApiParam(value = "轮播图uuid", required = true) String uuid) {
        carouselService.enable(uuid);
        return success();
    }

    @PutMapping("/disable/{uuid}")
    @ApiOperation("禁用轮播图")
    public ApiResponse<Void> disable(@PathVariable(value = "uuid", required = true) @ApiParam(value = "轮播图uuid", required = true) String uuid) {
        carouselService.disable(uuid);
        return success();
    }


}
